package cn.wbnyua.resume.core.security;

import cn.wbnyua.resume.mvc.domain.MySelf;
import cn.wbnyua.resume.mvc.service.v2.MySelfService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Component;

/**
 * 作者：wbnyua.
 * 创建时间: 2023/2/13 11:29.
 */

@Component
public class MyUserDetailService implements UserDetailsService {

    @Autowired
    private PasswordEncoder passwordEncoder;

    @Autowired
    private MySelfService mySelfService;

    @Override
    public UserDetails loadUserByUsername(String account) throws UsernameNotFoundException {

        MySelf mySelf = mySelfService.findByAccount(account);
        if (mySelf!=null){
            return new User(account,mySelf.getPassword(), AuthorityUtils.commaSeparatedStringToAuthorityList("admin"));
        }
        throw new UsernameNotFoundException("用户名或密码错误");

    }

}
